<template>
  <div class="table-frame com-full com-flex-c">
    <div class="title com-back com-center-c">
      <span>{{ title }}</span>
    </div>
    <div class="table com-back">
      <div class="top com-flex-r com-center-c">
        <div
          class=""
          v-for="(item, index) in column"
          :key="index"
          :style="{ width: style(item.width) }"
        >
          <span class="top-title">{{ item.label }}</span>
        </div>
      </div>
      <div class="body">
        <Vue3SeamlessScroll :list="row" :step="0.5" :hover="true">
          <div class="back" v-for="(item, index) in row" :key="index">
            <div class="tr com-flex-r">
              <div
                class="td"
                v-for="(use, index) in column"
                :key="index"
                :style="{ width: style(use.width) }"
              >
                <div
                  v-if="use.dataType === 'Grade'"
                  class="com-flex-r com-center-c"
                  :style="getStyle(use, item)"
                >
                  <div class="point" :style="getBack(use, item)"></div>
                  <div class="name-td com-slh">{{ item[use.props] }}</div>
                </div>
                <div v-else-if="use.dataType === 'Time'" :style="getStyle(use, item)">
                  <div class="name-td com-slh">
                    {{ isMockData ? time : item[use.props] }}
                  </div>
                </div>
                <div v-else-if="use.dataType === 'Sheet'" :style="getStyle(use, item)">
                  <div class="name-td com-slh">
                    {{ isMockData ? updateNum(item[use.props]) : item[use.props] }}
                  </div>
                </div>
                <div v-else :style="getStyle(use, item)">
                  <div class="name-td com-slh">{{ item[use.props] }}</div>
                </div>
              </div>
            </div>
          </div>
        </Vue3SeamlessScroll>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
import { computed, ref, onUnmounted, onMounted } from "vue";
import bus from "@/utils/bus.js";
const props = defineProps({
  title: {
    type: String,
    default: "告警数据",
  },
  column: {
    type: Array,
    default: () => [],
  },
  row: {
    type: Array,
    default: () => [],
  },
});

const style = (num) => {
  return `${num}%`;
};

const getStyle = (use, item) => {
  if (use.style) {
    return use.style(item[use.props], item);
  }
  return {};
};

const getBack = (use, item) => {
  if (use.Grade_type) {
    return use.Grade_type(item[use.props], item);
  }
};
const getText = (item, use) => {
  if (use.formatter) {
    return use.formatter(item[use.prop]);
  }
  return item[use.prop];
};
const time = ref();
const now = new Date();
const sheetNum = ref();
//修改时间
const changeTime = () => {
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0"); // 月份是从0开始的
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  sheetNum.value = String(now.getFullYear()).slice(2) + month + day;
  time.value = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
changeTime();

//修改流水号的时间部分
const updateNum = (num) => {
  let sheet = num.split("-");
  sheet[2] = sheetNum.value;
  sheet = sheet.join("-");
  return sheet;
};
let isMockData = ref(true);
onMounted(() => {
  bus.on("useMock", (e) => {
    isMockData.value = e;
  });
});
onUnmounted(() => {
  bus.off("useMock", (e) => {
    isMockData.value = e;
  });
});
</script>

<style lang="scss" scoped>
.table-frame {
  width: 90%;
  height: 80%;
  justify-content: space-between;

  .title {
    width: 100%;
    height: 36px;
    background: url("/imgs/14.png");
    font-size: 16px;
    font-family: Adobe Heiti Std, Adobe Heiti Std-R;
    font-weight: normal;
    text-align: left;
    color: #f2f5fe;
    line-height: 24px;

    span {
      margin-left: 15px;
    }
  }

  .table {
    width: 100%;
    height: 84%;
    background: url("/imgs/12.png");
    overflow: auto;
    pointer-events: all;
    display: flex;
    flex-direction: column;

    .top {
      margin: 0 0 10px 18px;
      width: 100%;
      height: 38px;

      .top-title {
        font-size: 18px;
        font-family: PingFang SC, PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #afbed8;
        line-height: 32px;
      }
    }

    .body {
      flex: 1;
      overflow: hidden;
      min-height: 0;
    }

    .back {
      margin-top: 5px;
      width: 100%;
      height: 38px;
      background: url("/imgs/13.png");

      .tr {
        margin-left: 18px;

        .td {
          font-size: 14px;
          font-family: PingFang SC, PingFang SC-Regular;
          font-weight: 400;
          text-align: left;
          color: #afbed8;
          line-height: 32px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;

          .point {
            margin-right: 6px;
            width: 5px;
            height: 5px;
            border-radius: 50%;
          }

          .name-td {
            font-size: calc(var(--fs) * 0.55);
            display: inline-block;
          }
        }
      }
    }
  }
}
</style>
